package mx.vlabs.soapdroid.wsdl2java.ui;

import mx.vlabs.soapdroid.wsdl2java.ui.exceptions.TaskExecutionException;

public abstract class AsyncTask implements Runnable{
	
	private Thread t;
	
	public AsyncTask() {
		this(true);
	}
	
	public AsyncTask(boolean autoExecute) {
		t = new Thread(this);
		
		if(autoExecute)
		start();
		
	}
	
	public void start(){
		beforeExecute();
		t.start();
	}
	
	public abstract void beforeExecute();
	public abstract void execute() throws TaskExecutionException;
	public abstract void onSuccess();
	public abstract void onError(TaskExecutionException e);
	public abstract void onComplete();

	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			execute();
			onSuccess();
		} catch (TaskExecutionException e) {
			// TODO Auto-generated catch block
			onError(e);
		} finally{
			onComplete();
		}
		
	}

}
